package com.cn.customer.housekeeping.housekeepingb.ui;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.cn.customer.housekeeping.housekeepingb.R;
import com.cn.customer.housekeeping.housekeepingb.bean.BaseResp;
import com.cn.customer.housekeeping.housekeepingb.bean.ImageInfo;
import com.cn.customer.housekeeping.housekeepingb.bean.SignResp;
import com.cn.customer.housekeeping.housekeepingb.biz.personcenter.IUserLoginView;
import com.cn.customer.housekeeping.housekeepingb.biz.personcenter.InterfacePresenter;
import com.cn.customer.housekeeping.housekeepingb.bridge.BridgeFactory;
import com.cn.customer.housekeeping.housekeepingb.bridge.Bridges;
import com.cn.customer.housekeeping.housekeepingb.bridge.cache.sharePref.EBSharedPrefManager;
import com.cn.customer.housekeeping.housekeepingb.citypicker.utils.StringUtils;
import com.cn.customer.housekeeping.housekeepingb.constant.URLUtil;
import com.cn.customer.housekeeping.housekeepingb.ui.adapter.ImgListAdapter;
import com.cn.customer.housekeeping.housekeepingb.ui.base.BaseActivity;
import com.cn.customer.housekeeping.housekeepingb.util.BizService;
import com.cn.customer.housekeeping.housekeepingb.util.KeyBoardUtil;
import com.cn.customer.housekeeping.housekeepingb.util.NavUtils;
import com.cn.customer.housekeeping.housekeepingb.util.PutObjectSample;
import com.cn.customer.housekeeping.housekeepingb.util.StringUtil;
import com.cn.customer.housekeeping.housekeepingb.util.ToastUtil;
import com.google.gson.Gson;
import com.tencent.cos.utils.FileUtils;

import java.io.File;
import java.util.ArrayList;


/***
 * 发布动态
 */
public class IdeaSwipeActivity extends BaseActivity implements ImgListAdapter.OnFooterViewClickListener, IUserLoginView {
    InterfacePresenter mInterfacePresenter;
    private ImageView addpic;
    private RecyclerView mRecyclerView;
    private LinearLayout mIdea_linearLayout;
    private ArrayList<ImageInfo> mSelectImgList = new ArrayList<ImageInfo>();
    private String mIdeaType;
    private static final int TEXT_LIMIT = 140;
    private static final int TEXT_REMIND = 10;
    private EBSharedPrefManager manager = BridgeFactory.getBridge(Bridges.SHARED_PREFERENCE);
    private String namePosition, addressPosition, LatPosition, LongPosition, personInfos;
    private LayoutInflater inflater;
    BizService bizService;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        setContentView(R.layout.compose_idea_layout);
        super.onCreate(savedInstanceState);

        //初始化 cosClient
        bizService = BizService.instance();
        bizService.init(getApplicationContext());
    }

    public void upload2() {


    }

    EditText mEditText;
    LinearLayout my_wallet;
    TextView addFeedNumChange;
    RelativeLayout rl_yingyetime;
    LinearLayout ll_fuwufanwei;

    @Override
    public void initViews() {
        mInterfacePresenter = new InterfacePresenter();
        mInterfacePresenter.attachView(this);
        inflater = LayoutInflater.from(this);
        addpic = (ImageView) findViewById(R.id.addpic);
        mRecyclerView = (RecyclerView) findViewById(R.id.ImgList);
        mIdea_linearLayout = (LinearLayout) findViewById(R.id.idea_linearLayout);
        addFeedNumChange = (TextView) findViewById(R.id.add_feed_num_change);
        mEditText = (EditText) findViewById(R.id.idea_content);
        my_wallet = (LinearLayout) findViewById(R.id.my_wallet);
        rl_yingyetime = (RelativeLayout) findViewById(R.id.rl_yingyetime);
        ll_fuwufanwei = (LinearLayout) findViewById(R.id.ll_fuwufanwei);

    }

    @Override
    public void initListeners() {
        ll_fuwufanwei.setOnClickListener(this);
        addpic.setOnClickListener(this);
        my_wallet.setOnClickListener(this);
        rl_yingyetime.setOnClickListener(this);
        mEditText.addTextChangedListener(watcher);

    }


    @Override
    public void initData() {
        mInterfacePresenter.getGetsign();
        mEditText.setTag(false);
        mEditText.post(new Runnable() {
            @Override
            public void run() {
//                setLimitTextColor(mLimitTextView, mEditText.getText().toString());
                KeyBoardUtil.openKeybord(mEditText, IdeaSwipeActivity.this);
                mEditText.requestFocus();
            }
        });
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

    String content;

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tv_top_righttitle://发布动态

//                //批量上传图片
                final File[] files = new File[mSelectImgList.size()];
                for (int i = 0; i < mSelectImgList.size(); i++) {
                    files[i] = mSelectImgList.get(i).getImageFile();
                }
//                mInterfacePresenter.requestAsyncImgS(files, "filecontent", mSignResp.getSign());


                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        String filename = FileUtils.getFileName(files[0].getAbsolutePath());
                        String cosPath = "/" + filename; //cos 上的路径
                        PutObjectSample.putObjectForSamllFile(bizService, cosPath, files[0].getAbsolutePath(), "Te4GQan45nA0/78hBZGSKrm5MAFhPTEyNTMyMTAxNTMmaz1BS0lEV0Z1TzM2WTlHR01VNXRrV0d1TkRLUFZad0luTnJoSXEmZT0xNTEyOTkxNDczJnQ9MTUxMjk5MTM3MyZyPTMxNDQxMjI3NTYmZj0mYj1ob21l");
                    }
                }).start();


//
//
//                content = mEditText.getText().toString();//动态内容
//                if (mTagList.size() > 3) {
//                    ToastUtil.makeText(IdeaSwipeActivity.this, "不得大于三个标签");
//                    return;
//                }
//                if (StringUtil.isEmpty(content)) {
//                    ToastUtil.makeText(IdeaSwipeActivity.this, "动态内容不能为空");
//                    return;
//                }
//
//
//                if (StringUtil.isEmpty(namePosition) || StringUtil.isEmpty(LatPosition) || StringUtil.isEmpty(LongPosition)) {//地址名称
//                    ToastUtil.makeText(IdeaSwipeActivity.this, "未获取到位置信息");
//                    return;
//                }

                break;


//            case R.id.picture://选择图片
            case R.id.addpic://选择图片
                NavUtils.gotoAlbumSwipeActivity(IdeaSwipeActivity.this, mSelectImgList, true, 1000);
                break;
            case R.id.my_wallet://选择店铺
                NavUtils.gotoChoiceShopActivity(IdeaSwipeActivity.this, 1001);
                break;
            case R.id.rl_yingyetime://营业时间
                NavUtils.gotoChoiceBusinessTimeActivity(IdeaSwipeActivity.this, 1002);
                break;
            case R.id.ll_fuwufanwei://服务范围
                if (!StringUtil.isEmpty(namePosition) && !StringUtil.isEmpty(addressPosition) && !StringUtil.isEmpty(LatPosition)) {
                    NavUtils.gotoPlaceDetailsActivity(IdeaSwipeActivity.this, 1003, namePosition, addressPosition, LatPosition, LongPosition);
                } else {
                    NavUtils.gotoChoicePositionActivity(IdeaSwipeActivity.this, 1003);
                }


                break;

        }
        super.onClick(v);
    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        switch (requestCode) {
            case 1000:
                if (data != null) {
                    String path = (String) data.getExtras().get("select_result");
                    if (!StringUtils.isEmpty(path)) {
                        ImageInfo mImageInfo = new ImageInfo();
                        mImageInfo.setImageFile(new File(path));
                        mImageInfo.setIsSelected(true);
                        mSelectImgList.add(mImageInfo);
                        initImgList();
                    }
                    if (data.getParcelableArrayListExtra("selectImgList") != null) {
                        mSelectImgList = data.getParcelableArrayListExtra("selectImgList");
                        initImgList();
                    }
                }
                break;
            case 1003:
                if (data != null) {
                    namePosition = (String) data.getExtras().get("name");
                    addressPosition = (String) data.getExtras().get("address");
                    LatPosition = (String) data.getExtras().get("Lat");
                    LongPosition = (String) data.getExtras().get("Long");

                }
                break;
            case 5000:

                break;


        }
    }


    public void initImgList() {
        mRecyclerView.setVisibility(View.VISIBLE);
        GridLayoutManager gridLayoutManager = new GridLayoutManager(IdeaSwipeActivity.this, 3);
        ImgListAdapter imgListAdapter = new ImgListAdapter(mSelectImgList, IdeaSwipeActivity.this, addpic);
        imgListAdapter.setOnFooterViewClickListener(this);
        mRecyclerView.setLayoutManager(gridLayoutManager);
        mRecyclerView.setAdapter(imgListAdapter);
        if (mSelectImgList != null && mSelectImgList.size() > 0) {
            addpic.setVisibility(View.GONE);
        } else {
            addpic.setVisibility(View.VISIBLE);
        }

    }


    @Override
    public void OnFooterViewClick() {
        NavUtils.gotoAlbumSwipeActivity(IdeaSwipeActivity.this, mSelectImgList, true, 1000);
    }

    private TextWatcher watcher = new TextWatcher() {
        private CharSequence inputString;

        @Override

        public void onTextChanged(CharSequence s, int start, int before, int count) {
            inputString = s;
        }

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {

        }

        @Override
        public void afterTextChanged(Editable s) {
            addFeedNumChange.setText(s.length() + "/" + "300字");
        }
    };


    @Override
    public void setHeader() {
        super.setHeader();
        rlTitle.setVisibility(View.VISIBLE);
        tvTopRighttitle.setVisibility(View.VISIBLE);
        tvTopRighttitle.setText("保存");
        tvTopTitle.setText("店铺设置");
        ivTopBack.setVisibility(View.VISIBLE);


    }


    @Override
    public void onError(BaseResp s) {
        if (s != null) {
            ToastUtil.makeText(IdeaSwipeActivity.this, s.getMessage());
        }
    }

    SignResp mSignResp;

    @Override
    public void onSuccess(BaseResp s) {

        if (s.getUrl().equals(URLUtil.url_get_getsign)) {//
            Gson gson = new Gson();
            mSignResp = gson.fromJson(s.getData(), SignResp.class);
            ToastUtil.makeText(IdeaSwipeActivity.this, "阿朵撒旦撒旦" + mSignResp.getSign());

        }
    }
}
